package com.yzw.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yzw.dao.SysRoleMapper;
import com.yzw.dto.DataGridResult;
import com.yzw.dto.QueryDTO;
import com.yzw.pojo.SysRole;
import com.yzw.pojo.SysUser;
import com.yzw.service.SysRoleService;
import com.yzw.utils.ShiroUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Athor:Yuan
 * @Date:2020/3/16$ 17:17$
 * Desc:
 */
@Service
public class SysRoleServiceImpl implements SysRoleService {

    @Autowired
    private SysRoleMapper sysRoleMapper;


    @Override
    public DataGridResult findPage(QueryDTO queryDTO) {
        PageHelper.offsetPage(queryDTO.getOffset(),queryDTO.getLimit());
        if (queryDTO.getSort()!=null && !queryDTO.getSort().equals("")){
            queryDTO.setSort("role_id");
        }
        List<SysRole> page = sysRoleMapper.findPage(queryDTO);
        PageInfo<SysRole> pageInfo = new PageInfo<>(page);
        DataGridResult dataGridResult = new DataGridResult(pageInfo.getTotal(),pageInfo.getList());
        return dataGridResult;
    }

    @Override
    public int save(SysRole sysRole) {
        //从shiro中获得用户实体
        SysUser userEntity = ShiroUtils.getUserEntity();
        sysRole.setCreateUserId(userEntity.getUserId());
        sysRole.setCreateTime(new Date());
        int i = sysRoleMapper.insertSelective(sysRole);

        return i;
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @Override
    public int deleteRole(List<Long> ids) {
      int i =  sysRoleMapper.deleteRole(ids);
        return i;
    }

    @Override
    public SysRole findRoleById(long roleId) {
        SysRole sysRole = sysRoleMapper.selectByPrimaryKey(roleId);
        return sysRole;
    }

    /**
     * 新增
     * @param sysRole
     */
    @Override
    public void update(SysRole sysRole) {
        sysRoleMapper.updateByPrimaryKeySelective(sysRole);
    }

    @Override
    public List<SysRole> roleAll() {
        return sysRoleMapper.selectByExample(null);
    }
}
